﻿
using System.Configuration;

namespace System
{
    /// <summary>
    /// 配置类
    /// </summary>
    public class ConfigHelper
    {
        private static Configuration.Configuration _configDic;
        /// <summary>
        /// 配置字典
        /// </summary>
        public static Configuration.Configuration ConfigDic => _configDic ?? (_configDic = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));

        /// <summary>
        /// 修改设置项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetConfig(string key, string value)
        {
            var conf = ConfigDic.AppSettings.Settings[key];
            if (conf == null)
                ConfigDic.AppSettings.Settings.Add(key, value);
            else conf.Value = value;
            ConfigDic.Save();
        }

        /// <summary>
        /// 获取设置项
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfig(string key)
        {
            var conf = ConfigDic.AppSettings.Settings[key];
            return conf?.Value;
        }

    }
}
